package com.fit.pages.wholesale;

import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.fit.annotations.AdminAccess;
import com.fit.annotations.UserAccess;
import com.fit.dao.wholesale.WholesaleDAO;
import com.fit.entities.Wholesale;


@AdminAccess
public class InsertWholesale {

	@Property
	@SessionState(create = false)
	private Wholesale wholesale;

	@Inject
	private WholesaleDAO wholesaleDAO;

	@Inject
	private Messages messages;

	@Persist
	@Property
	private String error;

	@Persist
	@Property
	private String confirmation;
	
//	@Property
//	@Persist
//	private VehicleType vehicleType;
//	
//	public SelectModel getTypes() {
//		return new EnumSelectModel(VehicleType.class, messages);
//	}
	
	void onPrepare() {
		wholesale = new Wholesale();
	}

	Object onSubmitFromCreateForm() {
		error = "";
		confirmation="";
		Wholesale temp = wholesaleDAO.findByName(wholesale
				.getName());

		if (temp == null) {
//			if (wholesaleType == null) {
//				error = "You didn't choose vehicle type!";
//				return null;
//			}
//			vehicle.setTypeOfVehicle(vehicleType.toString());
			if (wholesaleDAO.save(wholesale)) {
				confirmation = "Wholesale successfully inserted!";
				return null;
			}
			confirmation = "Vehicle is null!";
			return null;
		}

		error = "Wholesale with this name "
				+ wholesale.getName() + " already exists!";

		return null;
	}

	@OnEvent(component = "ResetConfLink")
	Object onResetConfLink() {
		confirmation = "";
		return null;
	}

	@OnEvent(component = "ResetErrorLink")
	Object onResetErrorLink() {
		error = "";
		return null;
	}

	public boolean isErrorNotNull() {
		if (error == null || error == "") {
			return false;
		}
		return true;
	}

	public boolean isConfirmationNotNull() {
		if (confirmation == null || confirmation == "") {
			return false;
		}
		return true;
	}

}
